package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("FoodbatchmaterialController")
public class FoodbatchmaterialController extends CmiEmaController {

	@RequestMapping("/listFoodbatchmaterials")
	public ModelAndView listFoodbatchmaterials(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodbatchmaterialPaginationService.updatePagination(request, pagination);
		Set<Foodbatchmaterial> foodbatchmaterials = foodbatchmaterialPaginationService
				.getPaginationFoodbatchmaterials(pagination);

		mav.addObject("foodbatchmaterials", foodbatchmaterials);
		mav.addObject("entityName", "foodbatchmaterial");
		mav.addObject("pagination", pagination);
		mav.setViewName("foodbatchmaterial/listFoodbatchmaterials.jsp");
		return mav;
	}

	public String listFoodbatchmaterials() {
		return "redirect:/listFoodbatchmaterials";
	}

	@RequestMapping("/viewFoodbatchmaterial")
	public ModelAndView viewFoodbatchmaterial(@RequestParam Integer foodbatchmaterialId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("foodbatchmaterial", foodbatchmaterialDAO.findFoodbatchmaterialByPrimaryKey(foodbatchmaterialId));
		mav.setViewName("foodbatchmaterial/viewFoodbatchmaterial.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoodbatchmaterials")
	public ModelAndView confirmDeleteFoodbatchmaterials(
			@RequestParam(value = "deleteFoodbatchmaterialsIds", required = true) Set<Integer> deleteFoodbatchmaterialsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Foodbatchmaterial> deleteFoodbatchmaterials = foodbatchmaterialDAO
				.findFoodbatchmaterialsByIds(deleteFoodbatchmaterialsIds);
		Set<Foodbatchmaterial> referencedFoodbatchmaterials = foodbatchmaterialService
				.loadReferenceFoodbatchmaterials(deleteFoodbatchmaterials);
		boolean hasReferenced;
		if (referencedFoodbatchmaterials.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoodbatchmaterials);
			hasReferenced = false;
		} else {
			deleteFoodbatchmaterials.removeAll(referencedFoodbatchmaterials);
			mav.addObject("referencedEntitys", referencedFoodbatchmaterials);
			mav.addObject("deleteEntitys", deleteFoodbatchmaterials);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Foodbatchmaterial");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoodbatchmaterials");
		return mav;
	}

	@RequestMapping("/deleteFoodbatchmaterial")
	public String deleteFoodbatchmaterial(@RequestParam Integer foodbatchmaterialId) {
		Foodbatchmaterial foodbatchmaterial = foodbatchmaterialDAO
				.findFoodbatchmaterialByPrimaryKey(foodbatchmaterialId);
		foodbatchmaterialService.deleteFoodbatchmaterial(foodbatchmaterial);
		return "forward:/listFoodbatchmaterials";
	}

	@RequestMapping("/confirmDeleteFoodbatchmaterial")
	public ModelAndView confirmDeleteFoodbatchmaterial(@RequestParam Integer foodbatchmaterialId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("foodbatchmaterial", foodbatchmaterialDAO.findFoodbatchmaterialByPrimaryKey(foodbatchmaterialId));

		mav.setViewName("foodbatchmaterial/deleteFoodbatchmaterial.jsp");

		return mav;
	}

	@RequestMapping("/deleteFoodbatchmaterials")
	public String deleteFoodbatchmaterials(
			@RequestParam(value = "deleteFoodbatchmaterialsId", required = true) Set<Integer> deleteFoodbatchmaterialsId) {
		foodbatchmaterialService.deleteFoodbatchmaterials(deleteFoodbatchmaterialsId);
		return "forward:/listFoodbatchmaterials";
	}

	@RequestMapping("/editFoodbatchmaterial")
	public ModelAndView editFoodbatchmaterial(@RequestParam Integer foodbatchmaterialId) {

		ModelAndView mav = new ModelAndView();

		Foodbatchmaterial foodbatchmaterial = foodbatchmaterialDAO
				.findFoodbatchmaterialByPrimaryKey(foodbatchmaterialId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("foodbatchmaterial", foodbatchmaterial);

		// 把所有的引用foodbatchmaterial对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodbatchmaterialService.loadFoodbatchmaterialObjectReferencedSet(foodbatchmaterial));

		// 将编辑页面foodbatchmaterial添加到mav中
		mav.setViewName("foodbatchmaterial/editFoodbatchmaterial.jsp");
		return mav;
	}

	@RequestMapping("/newFoodbatchmaterial")
	public ModelAndView newFoodbatchmaterial() {
		ModelAndView mav = new ModelAndView();
		Foodbatchmaterial foodbatchmaterial = new Foodbatchmaterial();

		mav.addObject("foodbatchmaterial", foodbatchmaterial);

		// 把所有的引用foodbatchmaterial对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodbatchmaterialService.loadFoodbatchmaterialObjectReferencedSet(foodbatchmaterial));

		mav.addObject("newFlag", true);
		mav.setViewName("foodbatchmaterial/editFoodbatchmaterial.jsp");

		return mav;
	}

	@RequestMapping("/saveFoodbatchmaterial")
	public ModelAndView saveFoodbatchmaterial(@ModelAttribute Foodbatchmaterial foodbatchmaterial) {

		ModelAndView mav = new ModelAndView();

		Foodbatch foodbatchTemp = foodbatchmaterial.getFoodbatch();
		Foodbatch foodbatch = foodbatchTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(foodbatchDAO.findFoodbatchById(foodbatchTemp.getId()));
		Material materialTemp = foodbatchmaterial.getMaterial();
		Material material = materialTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(materialDAO.findMaterialById(materialTemp.getId()));

		Foodbatchmaterial existingFoodbatchmaterial = CmiHibernateProxyUtil
				.deproxy(foodbatchmaterialDAO.findFoodbatchmaterialByPhysicalPrimaryKey(foodbatch.getId(),
						material.getId(), foodbatchmaterial.getContent()));

		foodbatchmaterial.setFoodbatch(foodbatch);
		foodbatchmaterial.setMaterial(material);
		// 新建记录
		if (foodbatchmaterial.getId() == null) {
			if (foodbatchmaterial.equals(existingFoodbatchmaterial)) {

				mav.addAllObjects(foodbatchmaterialService.loadFoodbatchmaterialClassReferencedSet(foodbatchmaterial));
				// mav = editContinue(mav, foodbatchmaterial);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodbatchmaterial/editFoodbatchmaterial.jsp");
			} else {
				foodbatchmaterialService.saveFoodbatchmaterial(foodbatchmaterial);
				mav.setViewName("forward:/listFoodbatchmaterials");
			}
		}
		// 编辑记录
		else {
			Foodbatchmaterial originFoodbatchmaterial = CmiHibernateProxyUtil
					.deproxy(foodbatchmaterialDAO.findFoodbatchmaterialByPrimaryKey(foodbatchmaterial.getId()));
			if (!foodbatchmaterial.equals(originFoodbatchmaterial)
					&& foodbatchmaterial.equals(existingFoodbatchmaterial)) {
				mav.addAllObjects(foodbatchmaterialService.loadFoodbatchmaterialClassReferencedSet(foodbatchmaterial));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodbatchmaterial/editFoodbatchmaterial.jsp");
			} else {
				foodbatchmaterialService.saveFoodbatchmaterial(foodbatchmaterial);
				mav.setViewName("forward:/listFoodbatchmaterials");
			}
		}
		return mav;
	}

	@RequestMapping("/foodbatchmaterialController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
